<?php
require_once 'Artdepot/Controller/Action.php';

class CriticsController extends Artdepot_Controller_Action
{

    public function init()
    {
		parent::init();
    }

    public function indexAction()
    {
    	
    	//advertisements here
    	$adTable = new Model_DbTable_Advertisement(); 
	    $this->view->ads = $adTable->fetchAll("position = 0 and enabled = 1", "display_order ASC");
    	
    	$page = intval($this->_getParam('page', 1));
    	if ($page<1) {
    		$page = 1;
    	}
    	$reviewersTable = new Model_DbTable_Reviewer();
    	$criticiseTable = new Model_DbTable_Criticise();
    	$totalItemsNum = $reviewersTable->getDefaultAdapter()->fetchOne("select count(*) from reviewer");
    	$perPage = 10;
		$offset = ($page-1)*10;
		$totalPages = ceil($totalItemsNum/$perPage);
		$reviewers = $reviewersTable->fetchAll(null, 'id desc', $perPage, $offset);
		foreach ($reviewers as $reviewer) {
			$reviewer->criticises = $criticiseTable->fetchAll("reviewer={$reviewer->id}", 'id desc', 3);
		}
		$this->view->title = 'Criticises - ';
		$this->view->reviewers = $reviewers;
		$this->view->pager = self::pager($page, $perPage, $totalItemsNum, '/critics?page={page}');
    }
    
    public function reviewerAction() {
    	
    	
    	//advertisements here
    	$adTable = new Model_DbTable_Advertisement(); 
	    $this->view->ads = $adTable->fetchAll("position = 0 and enabled = 1", "display_order ASC");
    	
    	$id = intval($this->_getParam('id'));
    	$reviewersTable = new Model_DbTable_Reviewer();
    	$criticiseTable = new Model_DbTable_Criticise();
    	$this->view->reviewer = $reviewer = $reviewersTable->find($id)->current();
    	$this->view->criticises = $criticises = $criticiseTable->fetchAll("reviewer={$id}", 'id desc');
    	$this->view->title = $reviewer->name . ' - ';
    }
    
    public function showAction(){
    	
    	
    	//advertisements here
    	$adTable = new Model_DbTable_Advertisement(); 
	    $this->view->ads = $adTable->fetchAll("position = 0 and enabled = 1", "display_order ASC");
    	
    	$id = intval($this->_getParam('id'));
    	$criticiseTable = new Model_DbTable_Criticise();
    	$this->view->criticise = $criticise = $criticiseTable->fetchRow("id={$id}");
    	
    	$this->view->title = $criticise->title . ' - ';
    }


}

